import login from '@/utils/login/index.js';
import session from '@/utils/session.js';

function enpty() {
	if (typeof obj == "undefined" || obj == null || obj == "") {
		return true;
	} else {
		return false;
	}
}

const state = {
	userInfo: session.get() ? session.get() : {}
}

const mutations = {
	//设置用户信息
	setUserInfo: (state, value) => {
		console.log("执行到状态管理", value);
		state.userInfo = value;
		session.set(value);
	},
	//用户退出
	onUserLogout(state, value) {
		state.userInfo = "";
		session.clear()
	},
}

const actions = {
	/**
	 * 授权操作
	 */
	async onAuthorization({
		dispatch,
		commit,
		state
	}, e) {
		console.log("我的用户数据", );
		const login_api = new login();
		//判断是否登录过
		if (enpty(state.userInfo)) {
			console.log("全新登录");
			const userInfo = await login_api.login();
			commit('setUserInfo', userInfo)
		}
		//全新小程序登录
		else {
			console.log("二次登录");
			const userInfo = await login_api.loginWithCode();
			commit('setUserInfo', userInfo)
		}
	},
	//编辑表单数据
	demo({
		dispatch,
		commit,
		state
	}, formData) {
		commit('setForm', formData)
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
